/*
* 
* Validates date
*/
function validateDates(fromDate, toDate, fromDateLabel, toDateLabel, isToCompare) {
	var failReason;
	if(!(failReason = validateDateField(fromDate, fromDateLabel)))
		if(!(failReason = validateDateField(toDate, toDateLabel)))
				failReason = null;
	if(!failReason && isToCompare) {
		failReason = new Date(fromDate) > new Date(toDate) ? fromDateLabel+" cannot be greater than " + toDateLabel : "";
	}
	return failReason;
}

function validateDateField(givenDate, label) {
	if (givenDate === '') return label + ' is required';
	
	var failureReason = null,
		index = givenDate.indexOf('/'),
		month = givenDate.substring(0, index),
		day = givenDate.substring(index+1, givenDate.indexOf('/', index+1)),
		d = new Date(givenDate);
	
	if (d.toString() === 'Invalid Date') return 'Invalid entry for ' + label;
	if (!isValidDay(day) || !isValidMonth(month)) return 'Please enter '+label+' in MM/dd/yyyy format';
	
	return failureReason;
}

function isValidDay(day) {
	if (day < 1 || day > 31) 
		return false;
	return true;
}

function isValidMonth(month) {
	if (month < 1 || month > 12) 
		return false;
	return true;
}
